package com.okjava.util;

import com.alibaba.fastjson.JSONObject;

/**
 * Description:
 * Created @version 1.0    2016/9/22 16:57 by Benny
 */
public final class ResponseUtil {

    public static <T> String handleSuccess(T data) {
        JSONObject result = new JSONObject();
        result.put("status", 200);
        result.put("data", convertData(data));
        result.put("result", "true");
        return result.toJSONString();
    }

    public static <T> String handleFailed(String msg) {
        JSONObject result = new JSONObject();
        result.put("status", 404);
        result.put("msg", msg);
        result.put("result", "false");
        return result.toJSONString();
    }

    /**
     * 处理已经转换成jsonString的字符串
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> String handleJsonSuccess(T data) {
        JSONObject result = new JSONObject();
        result.put("status", 200);
        result.put("data", data);
        result.put("result", "true");
        return result.toJSONString();
    }

    private static <T> String convertData(T data) {
        return JSONObject.toJSONString(data);
    }

    private ResponseUtil() {
    }
}
